/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.ipo.mm.web.util.pager;

import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.http.HttpServletRequest;
import org.ipo.mm.ejb.entities.Log;
import org.ipo.mm.ejb.facade.log.LogFacadeLocal;

/**
 *
 * @author makcro
 */
public class LogPager extends AbstractPager{

    public LogPager() {
        resultsTotal = lookupLogFacade().getCount();
    }


    @Override
    protected void injectData(HttpServletRequest request) {
        List<Log> logs = lookupLogFacade().findAll(firstResult, maxResultsPerPage);

        request.setAttribute("LOG_SEARCH_RESULTS", logs);
        request.setAttribute("LOG_SEARCH_PAGENUMBERS", generatePageNumbers());
        request.setAttribute("LOG_SEARCH_TOTAL_RESULTS", this.resultsTotal);
        request.setAttribute("LOG_SEARCH_SHOW_NEXT", new Boolean(this.showNext));
        request.setAttribute("LOG_SEARCH_SHOW_PREV", new Boolean(this.showPrev));
    }

    @Override
    protected void setPageSize() {
        maxResultsPerPage = 50;
    }

    private LogFacadeLocal lookupLogFacade() {
        try {
            Context c = new InitialContext();
            return (LogFacadeLocal) c.lookup("java:comp/env/LogFacade");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }
}
